package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.AdminBannerBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.AdminBannerListData;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.AdminBannerListResult;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.BannerAddForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.BannerDeleteForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.BannerQryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.BannerUpdateForm;
import com.cloudbroker.bcs.platform.ltb.middle.entity.Banner;
import com.cloudbroker.bcs.platform.ltb.middle.service.BannerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by daibd19671 on 2016/12/7.
 */
@Service
public class AdminBannerBizServiceImpl implements AdminBannerBizService{


    @Resource
    private BannerService bannerService;

    @Override
    public HSBCPSBackVO bannerAdd(BannerAddForm form) {
        Banner banner = new Banner();
        banner.setCompany_id(form.getCompany_id());
        banner.setSendercomp_id(form.getCompany_id());
        List<Banner> list = bannerService.list(banner);
        if (list.size()>=5){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.BANNER_FULL,HSBCPSBackVO.class);
        }

        Banner temp = new Banner();
        temp.setCompany_id(form.getCompany_id());
        List<Banner> allList = bannerService.list(temp);
        int maxLevel = allList.size();
        if (form.getLevel() > maxLevel + 1) {
            form.setLevel(maxLevel + 1);
        } else {
            //查询有没有当前等级的banner
            temp = new Banner();
            temp.setLevel(form.getLevel());
            temp.setCompany_id(form.getCompany_id());
            List<Banner> levelList = bannerService.list(temp);
            if (form.getLevel() != null && levelList != null && levelList.size() != 0) {
                //等级整体+1
                bannerService.updateBannerLevelAdd(form.getLevel(), list.size());
            }
        }


        banner.setTitle(form.getTitle());
        banner.setUrl(form.getUrl());
        banner.setImage(form.getImage());
        banner.setLevel(form.getLevel());
        banner.setCreate_time(new Date().getTime());
        bannerService.save(banner);
        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }

    @Override
    public HSBCPSBackVO bannerDelete(BannerDeleteForm form) {
        Banner temp = new Banner();
        temp.setId(Long.valueOf(form.getId()));
        temp.setCompany_id(form.getCompany_id());
        temp.setSendercomp_id(form.getCompany_id());
        Banner banner =  bannerService.getByEntity(temp);
        if(banner==null){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.BANNER_NOT_EXIST,HSBCPSBackVO.class);
        }
        //查询有没有当前等级的banner
        temp = new Banner();
        temp.setLevel(banner.getLevel());
        List<Banner> list = bannerService.list(temp);
        if(banner.getLevel()!=null && list!=null && list.size()!=0){
            //查询所有
            temp = new Banner();
            list = bannerService.list(temp);
            //等级整体-1
            bannerService.updateBannerLevelReduce(Integer.valueOf(banner.getLevel()),list.size());
        }
        bannerService.deleteByEntity(banner);
        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }

    @Override
    public HSBCPSBackVO bannerUpdate(BannerUpdateForm form) {
        Banner temp = new Banner();
        temp.setId(Long.valueOf(form.getId()));
        temp.setCompany_id(form.getCompany_id());
        Banner banner =  bannerService.getByEntity(temp);
        if(banner==null){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PublicManageErrorInfo.BANNER_NOT_EXIST,HSBCPSBackVO.class);
        }
        if(form.getLevel()!=null){
            temp = new Banner();
            temp.setCompany_id(form.getCompany_id());
            List<Banner> allList = bannerService.list(temp);
            int maxLevel = allList.size();
            if (form.getLevel() > maxLevel) {
                form.setLevel(maxLevel);
            }
            //查询有没有当前等级的banner
            temp = new Banner();
            temp.setLevel(form.getLevel());
            List<Banner> list = bannerService.list(temp);
            if (list != null && list.size() != 0) {
                //比较目标等级与当前等级大小
                if (Long.valueOf(form.getLevel()) > Long.valueOf(banner.getLevel())) {
                    //等级整体-1
                    bannerService.updateBannerLevelReduce(Integer.valueOf(banner.getLevel()), Integer.valueOf(form.getLevel()));
                } else {
                    //等级整体+1
                    bannerService.updateBannerLevelAdd(Integer.valueOf(form.getLevel()), Integer.valueOf(banner.getLevel()));
                }
            }
        }
        banner.setTitle(form.getTitle());
        banner.setLevel(form.getLevel());
        banner.setImage(form.getImage());
        banner.setUrl(form.getUrl());
        banner.setUpdate_time(new Date().getTime());
        bannerService.update(banner);
        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }

    @Override
    public AdminBannerListResult bannerQry(BannerQryForm form) {
        Banner banner = new Banner();
        BeanUtils.copyProperties(form,banner);
        List<Banner> list = bannerService.list(banner);
        List<AdminBannerListData> backList = new ArrayList<>(list.size());
        for(Banner b : list){
            AdminBannerListData data = new AdminBannerListData();
            BeanUtils.copyProperties(b,data);
            backList.add(data);
        }
        AdminBannerListResult bannerListResult = new AdminBannerListResult();
        bannerListResult.setData_list(backList);
        return VOUtil.setSuccessResult(bannerListResult);
    }

}
